<?php
class GuidesController extends AppController {

	var $name = 'Guides';
	var $components = array('RequestHandler');

	function index() {
		$this->Guide->recursive = 1;
		$this->paginate = array('limit' => 10, 'order' => array('Guide.name' => 'ASC'));
		$created_guides = $this->paginate('Guide', array('Guide.created_by' => 1));
		$favorite_guides = $this->paginate('Guide', array('Guide.created_by <>' => 1));
		$this->set(compact('created_guides','favorite_guides'));
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid guide', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('guide', $this->Guide->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Guide->create();
			if ($this->Guide->saveAll($this->data, array('validate' => 'first'))) {
				$this->layout='ajax';
				$this->autoRender=false;
				echo json_encode(array('success' => 1, 'guide_id' => $this->Guide->id));
				exit;
			} else {
				header("HTTP/1.0 404 Not Found");
				$errors=array();
				foreach($this->data as $k => $v) {
					if($k==$this->modelNames[0]) {
						$model_errors=$this->{$k}->validationErrors;
					} else {
						$model_errors=$this->{$this->modelNames[0]}->{$k}->validationErrors;
					}
					$errors[$k]=$model_errors;
				}
				echo json_encode(array('errors' => $errors));
				exit;
			}
		}

		/*
		$markers = $this->Guide->Marker->find('list');
		$privacies = $this->Guide->Privacy->find('list');
		$favoriteGuides = $this->Guide->FavoriteGuide->find('list');
		$users = $this->Guide->User->UserProfile->find('list', array('fields' => array('UserProfile.user_id','UserProfile.name')));
		$venues = $this->Guide->Venue->find('list');
		$this->set(compact('markers', 'privacies', 'favoriteGuides', 'users', 'venues'));
		*/
	}

	function edit($id = null) {
		
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid guide', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Guide->saveAll($this->data, array('validate' => 'first'))) {
				$this->Session->setFlash(__('The guide has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				header("HTTP/1.0 404 Not Found");
				header('Content-type: application/json');
				$errors=array();
				foreach($this->data as $k => $v) {
					if($k==$this->modelNames[0]) {
						$model_errors=$this->{$k}->validationErrors;
					} else {
						$model_errors=$this->{$this->modelNames[0]}->{$k}->validationErrors;
					}
					$errors[$k]=$model_errors;
				}
	
				$this->Session->setFlash(__('The guide could not be saved. Please, try again.', true));
				echo json_encode(array('errors' => $errors));
				exit;
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Guide->read(null, $id);
		}
		/*
		$markers = $this->Guide->Marker->find('list');
		$privacies = $this->Guide->Privacy->find('list');
		$favoriteGuides = $this->Guide->FavoriteGuide->find('list');
		$users = $this->Guide->User->find('list');
		$venues = $this->Guide->Venue->find('list');
		$this->set(compact('markers', 'privacies', 'favoriteGuides', 'users', 'venues'));
		*/
		$this->render('add');
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for guide', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Guide->delete($id)) {
			$this->Session->setFlash(__('Guide deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Guide was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	Additional functions for Browse tab
	**/
	
	function browse_index() {
		$this->setAction('index');
	}
	
	function browse_add() {
		$this->setAction('add');
	}
	
	function browse_view($id = null) {
		$this->setAction('view', $id);
	}
	
	function browse_edit($id = null) {
		$this->setAction('edit', $id);
	}

}
